package com.ctgu.rabbitmq.rabbitmq.exchange.fanoutExchange;

import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ ClassName FanoutExchangeConsumer
 * @ Author Alex2
 * @ Date 2025/8/14 14:45
 **/
@Log4j2
@Component
public class FanoutExchangeConsumer {

    private final static String TAG = "Alex";

    // 监听队列
    @RabbitListener(queues = FanoutExchangeConfig.QUEUE_A, ackMode = "AUTO")
    public void receiveFromQueueA(String message) {
        log.debug("{} : Queue A 收到消息 : {}", TAG, message);
    }

    @RabbitListener(queues = FanoutExchangeConfig.QUEUE_B, ackMode = "MANUAL")
    public void receiveFromQueueB(String msg, Message message, Channel channel) throws IOException {
        try {
            log.debug("{} : Queue B 收到消息 : {}", TAG, msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);           // 手动确认
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);// 处理失败，不批量拒绝，将消息重新放回队列
        }
    }
}